package com.yiqi.lottery.feature.lottery.data;

import android.util.Log;

import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.lottery.entity.bo.ConfirmLotteryBo;
import com.yiqi.lottery.entity.bo.SelectItem;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.tencent.bugly.beta.tinker.TinkerApplicationLike.TAG;

public class LotteryData {

    public static String[] getLottery(List<SelectItem> selectItems, boolean isDantuo) {
        if (selectItems == null) {
            return null;
        }

        String qian = "";
        String hou = "";

        StringBuilder qianBuilder = new StringBuilder();
        StringBuilder houBuilder = new StringBuilder();

        StringBuilder qianDanBuilder = new StringBuilder();
        StringBuilder qianTuoBuilder = new StringBuilder();
        StringBuilder houDanBuilder = new StringBuilder();
        StringBuilder houTuoBuilder = new StringBuilder();
        for (int i = 0; i < selectItems.size(); i++) {
            if (selectItems.get(i).isDantuo()) {
                if (selectItems.get(i).getDantuoType() == 1) {
                    qianDanBuilder.append(+selectItems.get(i).getName() <= 9 ? "0" + selectItems.get(i).getName() + "  " : selectItems.get(i).getName() + "  ");
                } else if (selectItems.get(i).getDantuoType() == 2) {
                    qianTuoBuilder.append(selectItems.get(i).getName() <= 9 ? "0" + selectItems.get(i).getName() + "  " : selectItems.get(i).getName() + "  ");
                } else if (selectItems.get(i).getDantuoType() == 3) {
                    houDanBuilder.append(selectItems.get(i).getName() <= 9 ? "0" + selectItems.get(i).getName() + "  " : selectItems.get(i).getName() + "  ");
                } else if (selectItems.get(i).getDantuoType() == 4) {
                    houTuoBuilder.append(selectItems.get(i).getName() <= 9 ? "0" + selectItems.get(i).getName() + "  " : selectItems.get(i).getName() + "  ");
                }

            } else {

                if (selectItems.get(i).getType() == 1) {
                    qianBuilder.append(selectItems.get(i).getName() <= 9 ? "0" + selectItems.get(i).getName() + "  " : selectItems.get(i).getName() + "  ");
                } else if (selectItems.get(i).getType() == 2) {
                    houBuilder.append(selectItems.get(i).getName() <= 9 ? "0" + selectItems.get(i).getName() + "  " : selectItems.get(i).getName() + "  ");
                }
            }
        }

        if (isDantuo) {
            qian = "(  " + qianDanBuilder.toString() + ")  " + qianTuoBuilder.toString();
            hou = "(  " + houDanBuilder.toString() + ")  " + houTuoBuilder.toString();
        } else {
            qian = qianBuilder.toString();
            hou = houBuilder.toString();
        }


        return new String[]{qian, hou};
    }


    /**
     * 获取提交订单参数
     */

    public static String getLottoOrder(List<ConfirmLotteryBo> lotteryOpes) {
        if (lotteryOpes == null) return null;
        String str = "";
        String qian = "";
        String hou = "";
        for (int i = 0; i < lotteryOpes.size(); i++) {
            ConfirmLotteryBo confirmLotteryBo = lotteryOpes.get(i);
            if (confirmLotteryBo.getIsdantuo()) {
                qian = confirmLotteryBo.getName().replaceAll("  ", ",").replaceAll("\\(\\,", "").replaceAll("\\,\\)\\,", "+");
                hou = confirmLotteryBo.getHouName().replaceAll("  ", ",").replaceAll("\\(\\,", "").replaceAll("\\,\\)\\,", "+");
            } else {
                qian = confirmLotteryBo.getName().replaceAll(" +", ",");
                hou = confirmLotteryBo.getHouName().replaceAll(" +", ",");
            }

            str += qian.substring(0, qian.length() - 1) + "|" + hou.substring(0, hou.length() - 1) + ";";
        }
        return str.substring(0, str.length() - 1);
    }

    /**
     * 计算组合数，即C(n, m) = n!/((n-m)! * m!)
     *
     * @param n
     * @param m
     * @return
     */
    public static int combination(int n, int m) {
        return (n >= m) ? factorial(n) / factorial(n - m) / factorial(m) : 0;
    }

    /**
     * 计算阶乘数，即n! = n * (n-1) * ... * 2 * 1
     *
     * @param n
     * @return
     */
    public static int factorial(int n) {
        return (n > 1) ? n * factorial(n - 1) : 1;
    }

    /**
     * 组合选择
     */
    public static int combinationSelect(int qian, int hou) {
        if (qian < 5) {
            return 0;
        }
        if (hou < 2) {
            return 0;
        }
        int qianNum = (int) combination(qian, 5);

        int houNum = (int) combination(hou, 2);

        return qianNum * houNum;

    }

    /**
     * 大乐透胆拖注数
     *
     * @param map
     * @param fore_num
     * @param back_num
     * @return
     */
    public static int calculateDaleDanTuo(HashMap<Integer, Vector<String>> map, int fore_num, int back_num) {
        int betCount = 0;
        int danNum = map.get(0) == null ? 0 : map.get(0).size();
        int tuoNum = map.get(2) == null ? 0 : map.get(2).size();
        int backDanNum = map.get(1) == null ? 0 : map.get(1).size();
        int backTuoNum = map.get(3) == null ? 0 : map.get(3).size();
        if (danNum + tuoNum < fore_num || backDanNum + backTuoNum < back_num) {
            betCount = 0;
        } else {
            betCount = combination(tuoNum, fore_num - danNum) * combination(backTuoNum, back_num - backDanNum);
        }
        Logger.i("", "DaleDanTuo betCount:" + betCount);
        return betCount;
    }

    /**
     * 获取首页投注数据
     */

    public static String getDLTData(String[] data) {
        if (data == null) return null;
        String qian = data[0].replaceAll("  ", ",");
        String hou = data[1].replaceAll("  ", ",");
        qian = qian.substring(0, qian.length() - 1);
        hou = hou.substring(0, hou.length() - 1);
        return qian + "|" + hou;
    }

    /**
     * 双色球大乐透 单式复式注数
     *
     * @param map
     * @param fore_num
     * @param back_num
     * @return
     */
    private static int calculateShuangSeDale(HashMap<Integer, Vector<String>> map, int fore_num, int back_num) {
        int foreSelectNum = 0;
        int backSelectNum = 0;
        Vector<String> v0 = map.get(0);
        Vector<String> v = map.get(1);
        if (v0 != null) {
            foreSelectNum = v0.size();
        }
        if (v != null) {
            backSelectNum = v.size();
        }
        int betCount = combination(foreSelectNum, fore_num) * combination(backSelectNum, back_num);
        return betCount;
    }

    /**
     * 组合 从m中选出n个
     *
     * @param m
     * @param n
     * @return
     */
    public static int combination1(int m, int n) {
        if (m < n) {
            return 0;
        } else {
            return factorial1(m - n + 1, m) / factorial1(1, n);
        }
    }

    /**
     * 计算start到stop阶乘 正整数
     *
     * @param start
     * @param stop
     * @return
     */
    public static int factorial1(int start, int stop) {
        int total = 1;
        for (int i = start; i <= stop; i++) {
            total = total * i;
        }
        return total;
    }

    /**
     * 大乐透胆拖注数
     *
     * @param
     * @param fore_num
     * @param back_num
     * @return
     */
    public static int calculateDaleDanTuo(int danNum, int tuoNum, int backDanNum, int backTuoNum, int fore_num, int back_num) {
        int betCount = 0;
        if (danNum + tuoNum < fore_num || backDanNum + backTuoNum < back_num) {
            betCount = 0;
        } else {
            betCount = combination1(tuoNum, fore_num - danNum) * combination1(backTuoNum, back_num - backDanNum);
        }
        return betCount;
    }
}
